package com.alex.projecthelper.base

import android.app.Application
import com.blankj.utilcode.util.Utils


import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import mvvm.base.helper.base.MvvmHelper
import mvvm.base.helper.base.appContext
import mvvm.base.helper.ext.getColorExt
import mvvm.base.helper.widget.state.BaseEmptyCallback
import mvvm.base.helper.widget.state.BaseErrorCallback
import mvvm.base.helper.widget.state.BaseLoadingCallback
import rxhttp.RxHttpPlugins
import rxhttp.wrapper.cahce.CacheMode
import java.io.File

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        Utils.init(this)
        MvvmHelper.init(this, true)
        SmartRefreshLayout.setDefaultRefreshInitializer { context, layout ->
            //设置 SmartRefreshLayout 通用配置
//            layout.setEnableScrollContentWhenLoaded(true)//是否在加载完成时滚动列表显示新的内容
            layout.setFooterTriggerRate(0.6f)
        }
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
            //设置 Head
            ClassicsHeader(context).apply {
                setAccentColor(getColorExt(android.R.color.background_dark))
            }
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
            //设置 Footer
            ClassicsFooter(context).apply {
                setAccentColor(getColorExt(android.R.color.background_dark))
            }
        }
        //注册界面状态管理
        LoadSir.beginBuilder()
            .addCallback(BaseErrorCallback())
            .addCallback(BaseEmptyCallback())
            .addCallback(BaseLoadingCallback())
            .setDefaultCallback(SuccessCallback::class.java)
            .commit()

        RxHttpPlugins
            .init(NetHttpClient.getDefaultOkHttpClient().build())
            .setCache(
                File(appContext.externalCacheDir, "HttpCookie"),
                100 * 1024 * 1024,
                CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE,
                2 * 24 * 60 * 60 * 1000
            )
            .setDebug(true)
    }
}